from bs4 import BeautifulSoup
import requests

'''
图片一般都是比较难抓的
因为图片大多是是从js进行加载的，不能直接抓取
'''

url = 'https://www.tripadvisor.cn/'
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text, 'lxml')

image = soup.select(
    '#popularDestinations > div.section > ul.regionContent > li.active > ul > li > a > span.thumbCrop > img')
country = soup.select(
    '#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.title > a.countryName')
city = soup.select(
    '#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.title > a.cityName')
hotel = soup.select(
    '#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.counts > span.hotelsCount > a')
scenic_spot = soup.select('#popularDestinations > div.section > ul.regionContent > ' +
                          'li.active > ul > li > div.counts > span.attractionCount > a')
restaurant = soup.select(
    '#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.counts > span.eateryCount > a')

info = []




for image_item, city_item, country_item, hotel_item, scenic_spot_item, restaurant_item in zip(image, city, country,
                                                                                              hotel, scenic_spot,
                                                                                              restaurant):
    data = {
        'image': image_item.get('src'),
        'country': country_item.getText(),
        'city': city_item.getText(),
        'hotel': hotel_item.getText().split()[1],  # 以空格区分，获取第二个数据
        'scenicSpot': scenic_spot_item.getText().split()[1],
        'restaurant': restaurant_item.getText().split()[1],
    }
    info.append(data)

print(info)
